/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

const BusinessNetworkCardStore = require('../../lib/cardstore/businessnetworkcardstore');
const IdCard = require('../../lib/idcard');

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
chai.should();

describe('BusinessNetworkCardStore', function() {
    describe('#getDefaultCardName', function() {
        it('should return name based on card name and business network name for user card', () => {
            const metadata = { userName: 'conga', businessNetwork: 'penguin-network' };
            const connectionProfile = { name: 'profile-name' };
            const card = new IdCard(metadata, connectionProfile);
            const result = BusinessNetworkCardStore.getDefaultCardName(card);
            result.should.include(metadata.userName).and.include(metadata.businessNetwork);
        });

        it('should return name based on card name and connection profile name for PeerAdmin card', () => {
            const metadata = { userName: 'PeerAdmin', roles: [ 'PeerAdmin', 'ChannelAdmin' ] };
            const connectionProfile = { name: 'profile-name' };
            const card = new IdCard(metadata, connectionProfile);
            const result = BusinessNetworkCardStore.getDefaultCardName(card);
            result.should.include(metadata.userName).and.include(connectionProfile.name);
        });

        it('should update the name given in the connection profile if invalid', () => {
            const metadata = { userName: 'PeerAdmin', roles: [ 'PeerAdmin', 'ChannelAdmin' ] };
            const connectionProfile = { name: 'profile-name_that\'s got spaces and &^%$# characters' };
            const card = new IdCard(metadata, connectionProfile);
            const result = BusinessNetworkCardStore.getDefaultCardName(card);
            result.should.include(metadata.userName).and.include('profile-name_thats-got-spaces-and--characters');
        });
    });

    describe('#get', function() {
        it('should throw as abstract method', function() {
            const store = new BusinessNetworkCardStore();
            return store.get().should.be.rejectedWith(/Abstract/);
        });
    });

    describe('#put', function() {
        it('should throw as abstract method', function() {
            const store = new BusinessNetworkCardStore();
            return store.put().should.be.rejectedWith(/Abstract/);
        });
    });

    describe('#has', function() {
        it('should throw as abstract method', function() {
            const store = new BusinessNetworkCardStore();
            return store.has().should.be.rejectedWith(/Abstract/);
        });
    });

    describe('#getAll', function() {
        it('should throw as abstract method', function() {
            const store = new BusinessNetworkCardStore();
            return store.getAll().should.be.rejectedWith(/Abstract/);
        });
    });

    describe('#delete', function() {
        it('should throw as abstract method', function() {
            const store = new BusinessNetworkCardStore();
            return store.delete().should.be.rejectedWith(/Abstract/);
        });
    });

    describe('#getWallet', function() {
        it('should throw as abstract method', function() {
            const store = new BusinessNetworkCardStore();
            return store.getWallet().should.be.rejectedWith(/Abstract/);
        });
    });
});
